// pages/login/login.js
import {getcode,login as login_user} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'18522374667',
    code:'',
    time:60
  },
 //发送验证码
  async sendcode(){
    if (!this.validatePhone(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if(this.data.time !== 60){
      return
    }
    const {data:res} = await getcode({phone:this.data.phone})
 
    if(res.data.code === 200){
      wx.showToast({
        title: '验证码已发送',
        icon: 'success',
        duration: 2000,
        mask:true
      });
      
      var inter = setInterval(() => {
        this.setData({
          time: this.data.time - 1
        })
        if (this.data.time === 0) {
          clearInterval(inter)
          this.setData({
            time: 60,
          })
        }
      }, 1000)
    }else{
      wx.showToast({
        title: '发送失败',
        icon: 'none',
        duration: 2000,
        mask:true
      });
    }
  },
  //登入
  async login(e){

    const {phone,code} = e.detail.value
    if (!this.validatePhone(phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if(!this.validateCode(code)){
      wx.showToast({
        title: '验证码格式不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    const {data:res} = await login_user({phone,code})
   
    if(res.data.code == 200){
      wx.setStorageSync('token', res.data.token)
      wx.setStorageSync('nickname', res.data.nickname)
      const firstPart = phone.substring(0, 3);
      const lastPart = phone.substring(7);
      const hiddenPart = "****";
      let newphone = firstPart + hiddenPart + lastPart
      wx.setStorageSync('phone', newphone)
      wx.setStorageSync('user_img', res.data.user_img)
      this.setData({
        phone:'',
        code:''
      })
      wx.showToast({
        title: res.data.msg,
        icon: 'success',
        duration: 2000,
        mask:true
      });
      setTimeout(()=>{
        wx.switchTab({
          url:'/pages/index/index'
        })
      },2000)
    }else{
      wx.showToast({
        title: res.data.msg,
        icon: 'error',
        duration: 2000,
        mask:true
      });
    }
  },
  //验证手机号符合格式
  validatePhone: function(phone) {
    const phoneRegex = /^1[3-9]\d{9}$/;
    return phoneRegex.test(phone);
  },
  //验证验证码
  validateCode: function(code) {
    return code && code.length === 4;
  },
  getphone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  onLoad(options) {
    //this.sendcode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})